home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / Windows / WGL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-16  |  10.7 KB  |  431 lines

  1. /* $Id: wgl.c,v 1.3 1998/09/16 02:46:56 brianp Exp $ */
  2.  
  3. /*
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. */
  19.  
  20. /*
  21. * File name     : wgl.c
  22. * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
  23. * Some things originated from the 3Dfx WGL functions
  24. */
  25.  
  26. /*
  27.  * $Log: wgl.c,v $
  28.  * Revision 1.3  1998/09/16 02:46:56  brianp
  29.  * added RCS Id and Log tokens
  30.  *
  31.  */
  32.  
  33. #ifdef WIN32
  34.  
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38.  
  39. #include <GL/gl.h>
  40. #include <GL/glu.h>
  41.  
  42. #ifdef __cplusplus
  43. }
  44. #endif
  45.  
  46. #include <stdio.h>
  47. #include <windows.h>
  48. #include <tchar.h>
  49. #include "wmesadef.h"
  50. #include "GL/wmesa.h"
  51. #include "types.h"
  52.  
  53. #define MAX_MESA_ATTRS    20
  54.  
  55. struct __extensions__
  56. {
  57.     PROC    proc;
  58.     char    *name;
  59. };
  60.  
  61. struct __pixelformat__
  62. {
  63.     PIXELFORMATDESCRIPTOR    pfd;
  64.     GLboolean doubleBuffered;
  65. };
  66.  
  67. struct __extensions__    ext[] = {
  68.  
  69. #ifdef GL_EXT_polygon_offset
  70.     {    (PROC)glPolygonOffsetEXT,        "glPolygonOffsetEXT"            },
  71. #endif
  72.     {    (PROC)glBlendEquationEXT,        "glBlendEquationEXT"            },
  73.     {    (PROC)glBlendColorEXT,            "glBlendColorExt"            },
  74.     {    (PROC)glVertexPointerEXT,        "glVertexPointerEXT"            },
  75.     {    (PROC)glNormalPointerEXT,        "glNormalPointerEXT"            },
  76.     {    (PROC)glColorPointerEXT,        "glColorPointerEXT"            },
  77.     {    (PROC)glIndexPointerEXT,        "glIndexPointerEXT"            },
  78.     {    (PROC)glTexCoordPointerEXT,        "glTexCoordPointer"            },
  79.     {    (PROC)glEdgeFlagPointerEXT,        "glEdgeFlagPointerEXT"                },
  80.     {    (PROC)glGetPointervEXT,            "glGetPointervEXT"            },
  81.     {    (PROC)glArrayElementEXT,        "glArrayElementEXT"            },
  82.     {    (PROC)glDrawArraysEXT,            "glDrawArrayEXT"            },
  83.     {    (PROC)glAreTexturesResidentEXT,    "glAreTexturesResidentEXT"                    },
  84.     {    (PROC)glBindTextureEXT,            "glBindTextureEXT"            },
  85.     {    (PROC)glDeleteTexturesEXT,        "glDeleteTexturesEXT"            },
  86.     {    (PROC)glGenTexturesEXT,            "glGenTexturesEXT"            },
  87.     {    (PROC)glIsTextureEXT,            "glIsTextureEXT"            },
  88.     {    (PROC)glPrioritizeTexturesEXT,    "glPrioritizeTexturesEXT"                },
  89.     {    (PROC)glCopyTexSubImage3DEXT,    "glCopyTexSubImage3DEXT"                },
  90.     {    (PROC)glTexImage3DEXT,            "glTexImage3DEXT"            },
  91.     {    (PROC)glTexSubImage3DEXT,        "glTexSubImage3DEXT"            },
  92. };
  93.  
  94. int                qt_ext = sizeof(ext) / sizeof(ext[0]);
  95.  
  96. struct __pixelformat__    pix[] =
  97. {
  98.     {    {    sizeof(PIXELFORMATDESCRIPTOR),    1,
  99.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
  100.         PFD_TYPE_RGBA,
  101.         24,    8,    0,    8,    8,    8,    16,    8,    24,
  102.         0,    0,    0,    0,    0,    16,    8,    0,    0,    0,    0,    0,    0 },
  103.         GL_TRUE
  104.     },
  105.     {    {    sizeof(PIXELFORMATDESCRIPTOR),    1,
  106.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT,
  107.         PFD_TYPE_RGBA,
  108.         24,    8,    0,    8,    8,    8,    16,    8,    24,
  109.         0,    0,    0,    0,    0,    16,    8,    0,    0,    0,    0,    0,    0 },
  110.         GL_FALSE
  111.     },
  112. };
  113.  
  114. int                qt_pix = sizeof(pix) / sizeof(pix[0]);
  115.  
  116. typedef struct {
  117.     WMesaContext ctx;
  118.     HDC hdc;
  119. } MesaWglCtx;
  120.  
  121. #define MESAWGL_CTX_MAX_COUNT 20
  122.  
  123. static MesaWglCtx wgl_ctx[MESAWGL_CTX_MAX_COUNT];
  124.  
  125. static unsigned ctx_count = 0;
  126. static unsigned ctx_current = 0;
  127. static unsigned curPFD = 0;
  128.  
  129. WINGDIAPI BOOL WINAPI wglCopyContext(HGLRC hglrcSrc,HGLRC hglrcDst,UINT mask)
  130. {
  131.     return(FALSE);
  132. }
  133.  
  134. WINGDIAPI HGLRC WINAPI wglCreateContext(HDC hdc)
  135. {
  136.     HWND        hWnd;
  137.     int i = 0;
  138.     if(!(hWnd = WindowFromDC(hdc)))
  139.     {
  140.         SetLastError(0);
  141.         return(NULL);
  142.     }
  143.     if (!ctx_count)
  144.     {
  145.         for(i=0;i<MESAWGL_CTX_MAX_COUNT;i++)
  146.         {
  147.             wgl_ctx[i].ctx = NULL;
  148.             wgl_ctx[i].hdc = NULL;
  149.         }
  150.     }
  151.     for( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  152.     {
  153.         if ( wgl_ctx[i].ctx == NULL )
  154.         {
  155.             wgl_ctx[i].ctx = WMesaCreateContext( hWnd, NULL, GL_TRUE,
  156.                 pix[curPFD-1].doubleBuffered );
  157.             if (wgl_ctx[i].ctx == NULL)
  158.                 break;
  159.             wgl_ctx[i].hdc = hdc;
  160.             ctx_count++;
  161.             return ((HGLRC)wgl_ctx[i].ctx);
  162.         }
  163.     }
  164.     SetLastError(0);
  165.     return(NULL);
  166. }
  167.  
  168. WINGDIAPI BOOL WINAPI wglDeleteContext(HGLRC hglrc)
  169. {
  170.     int i;
  171.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  172.     {
  173.         if ( wgl_ctx[i].ctx == hglrc )
  174.         {
  175.             WMesaMakeCurrent(hglrc);
  176.             WMesaDestroyContext();
  177.             wgl_ctx[i].ctx = NULL;
  178.             wgl_ctx[i].hdc = NULL;
  179.             ctx_count--;
  180.             return(TRUE);
  181.         }
  182.     }
  183.     SetLastError(0);
  184.     return(FALSE);
  185. }
  186.  
  187. WINGDIAPI HGLRC WINAPI wglCreateLayerContext(HDC hdc,int iLayerPlane)
  188. {
  189.     SetLastError(0);
  190.     return(NULL);
  191. }
  192.  
  193. WINGDIAPI HGLRC WINAPI wglGetCurrentContext(VOID)
  194. {
  195.    if (ctx_current < 0)
  196.       return 0;
  197.    else
  198.       return wgl_ctx[ctx_current].ctx;
  199. }
  200.  
  201. WINGDIAPI HDC WINAPI wglGetCurrentDC(VOID)
  202. {
  203.    if (ctx_current < 0)
  204.       return 0;
  205.    else
  206.       return wgl_ctx[ctx_current].hdc;
  207. }
  208.  
  209. WINGDIAPI BOOL WINAPI wglMakeCurrent(HDC hdc,HGLRC hglrc)
  210. {
  211.     int i;
  212.  
  213.     /* new code suggested by Andy Sy */
  214.     if (!hdc || !hglrc) {
  215.        WMesaMakeCurrent(NULL);
  216.        ctx_current = -1;
  217.        return TRUE;
  218.     }
  219.  
  220.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  221.     {
  222.         if ( wgl_ctx[i].ctx == hglrc )
  223.         {
  224.             wgl_ctx[i].hdc = hdc;
  225.             WMesaMakeCurrent( (WMesaContext) hglrc );
  226.             ctx_current = i;
  227.             return TRUE;
  228.         }
  229.     }
  230.     return FALSE;
  231. }
  232.  
  233. WINGDIAPI BOOL WINAPI wglShareLists(HGLRC hglrc1,HGLRC hglrc2)
  234. {
  235.     return(TRUE);
  236. }
  237.  
  238. WINGDIAPI BOOL WINAPI wglUseFontBitmapsA(HDC hdc,DWORD first,DWORD count,DWORD listBase)
  239. {
  240.     return(FALSE);
  241. }
  242.  
  243. WINGDIAPI BOOL WINAPI wglUseFontBitmapsW(HDC hdc,DWORD first,DWORD count,DWORD listBase)
  244. {
  245.     return(FALSE);
  246. }
  247.  
  248. WINGDIAPI BOOL WINAPI wglUseFontOutlinesA(HDC hdc,DWORD first,DWORD count,
  249.                                   DWORD listBase,FLOAT deviation,
  250.                                   FLOAT extrusion,int format,
  251.                                   LPGLYPHMETRICSFLOAT lpgmf)
  252. {
  253.     SetLastError(0);
  254.     return(FALSE);
  255. }
  256.  
  257. WINGDIAPI BOOL WINAPI wglUseFontOutlinesW(HDC hdc,DWORD first,DWORD count,
  258.                                   DWORD listBase,FLOAT deviation,
  259.                                   FLOAT extrusion,int format,
  260.                                   LPGLYPHMETRICSFLOAT lpgmf)
  261. {
  262.     SetLastError(0);
  263.     return(FALSE);
  264. }
  265.  
  266. WINGDIAPI BOOL WINAPI wglDescribeLayerPlane(HDC hdc,int iPixelFormat,
  267.                                     int iLayerPlane,UINT nBytes,
  268.                                     LPLAYERPLANEDESCRIPTOR plpd)
  269. {
  270.     SetLastError(0);
  271.     return(FALSE);
  272. }
  273.  
  274. WINGDIAPI int WINAPI wglSetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  275.                                        int iStart,int cEntries,
  276.                                        CONST COLORREF *pcr)
  277. {
  278.     SetLastError(0);
  279.     return(0);
  280. }
  281.  
  282. WINGDIAPI int WINAPI wglGetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  283.                                        int iStart,int cEntries,
  284.                                        COLORREF *pcr)
  285. {
  286.     SetLastError(0);
  287.     return(0);
  288. }
  289.  
  290. WINGDIAPI BOOL WINAPI wglRealizeLayerPalette(HDC hdc,int iLayerPlane,BOOL bRealize)
  291. {
  292.     SetLastError(0);
  293.     return(FALSE);
  294. }
  295.  
  296. WINGDIAPI BOOL WINAPI wglSwapLayerBuffers(HDC hdc,UINT fuPlanes)
  297. {
  298.     if( !hdc )
  299.     {
  300.         WMesaSwapBuffers();
  301.         return(TRUE);
  302.     }
  303.     SetLastError(0);
  304.     return(FALSE);
  305. }
  306.  
  307. WINGDIAPI int WINAPI wglChoosePixelFormat(HDC hdc,
  308.                                   CONST PIXELFORMATDESCRIPTOR *ppfd)
  309. {
  310.     int        i,best = -1,bestdelta = 0x7FFFFFFF,delta,qt_valid_pix;
  311.  
  312.     qt_valid_pix = qt_pix;
  313.     if(ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR) || ppfd->nVersion != 1)
  314.     {
  315.         SetLastError(0);
  316.         return(0);
  317.     }
  318.     for(i = 0;i < qt_valid_pix;i++)
  319.     {
  320.         delta = 0;
  321.         if(
  322.             (ppfd->dwFlags & PFD_DRAW_TO_WINDOW) &&
  323.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_WINDOW))
  324.             continue;
  325.         if(
  326.             (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) &&
  327.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_BITMAP))
  328.             continue;
  329.         if(
  330.             (ppfd->dwFlags & PFD_SUPPORT_GDI) &&
  331.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_GDI))
  332.             continue;
  333.         if(
  334.             (ppfd->dwFlags & PFD_SUPPORT_OPENGL) &&
  335.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_OPENGL))
  336.             continue;
  337.         if(
  338.             !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
  339.             ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != (pix[i].pfd.dwFlags & PFD_DOUBLEBUFFER)))
  340.             continue;
  341.         if(
  342.             !(ppfd->dwFlags & PFD_STEREO_DONTCARE) &&
  343.             ((ppfd->dwFlags & PFD_STEREO) != (pix[i].pfd.dwFlags & PFD_STEREO)))
  344.             continue;
  345.         if(ppfd->iPixelType != pix[i].pfd.iPixelType)
  346.             delta++;
  347.         if(delta < bestdelta)
  348.         {
  349.             best = i + 1;
  350.             bestdelta = delta;
  351.             if(bestdelta == 0)
  352.                 break;
  353.         }
  354.     }
  355.     if(best == -1)
  356.     {
  357.         SetLastError(0);
  358.         return(0);
  359.     }
  360.     return(best);
  361. }
  362.  
  363. WINGDIAPI int WINAPI wglDescribePixelFormat(HDC hdc,int iPixelFormat,UINT nBytes,
  364.                                     LPPIXELFORMATDESCRIPTOR ppfd)
  365. {
  366.     int        qt_valid_pix;
  367.  
  368.     qt_valid_pix = qt_pix;
  369.     if(iPixelFormat < 1 || iPixelFormat > qt_valid_pix || nBytes != sizeof(PIXELFORMATDESCRIPTOR))
  370.     {
  371.         SetLastError(0);
  372.         return(0);
  373.     }
  374.     *ppfd = pix[iPixelFormat - 1].pfd;
  375.     return(qt_valid_pix);
  376. }
  377.  
  378. /*
  379. * GetProcAddress - return the address of an appropriate extension
  380. */
  381. WINGDIAPI PROC WINAPI wglGetProcAddress(LPCSTR lpszProc)
  382. {
  383.     int        i;
  384.     for(i = 0;i < qt_ext;i++)
  385.         if(!strcmp(lpszProc,ext[i].name))
  386.             return(ext[i].proc);
  387.  
  388.         SetLastError(0);
  389.         return(NULL);
  390. }
  391.  
  392. WINGDIAPI int WINAPI wglGetPixelFormat(HDC hdc)
  393. {
  394.     if(curPFD == 0)
  395.     {
  396.         SetLastError(0);
  397.         return(0);
  398.     }
  399.     return(curPFD);
  400. }
  401.  
  402. WINGDIAPI BOOL WINAPI wglSetPixelFormat(HDC hdc,int iPixelFormat,
  403.                                 PIXELFORMATDESCRIPTOR *ppfd)
  404. {
  405.     int        qt_valid_pix;
  406.  
  407.     qt_valid_pix = qt_pix;
  408.     if(iPixelFormat < 1 || iPixelFormat > qt_valid_pix || ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR))
  409.     {
  410.         SetLastError(0);
  411.         return(FALSE);
  412.     }
  413.     curPFD = iPixelFormat;
  414.     return(TRUE);
  415. }
  416.  
  417. WINGDIAPI BOOL WINAPI wglSwapBuffers(HDC hdc)
  418. {
  419.    if (ctx_current < 0)
  420.       return FALSE;
  421.  
  422.    if(wgl_ctx[ctx_current].ctx == NULL) {
  423.       SetLastError(0);
  424.       return(FALSE);
  425.    }
  426.    WMesaSwapBuffers();
  427.    return(TRUE);
  428. }
  429.  
  430. #endif /* WIN32 */
  431.